package com.xone.xapp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class SubscribeUserAdapter extends BasicListAdapter<SubscribeListUserHolder> {
	
	protected Context context;
	protected LayoutInflater mInflater;
	public SubscribeUserAdapter(Context context, List<JSONObject> list) {
		this.context = context;
		this.list = initWithJsonList(list);
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public List<SubscribeListUserHolder> initWithJsonList(List<JSONObject> list) {
		if (JSONUtils.isNullOrEmptyList(list)) {
			return Collections.emptyList();
		}
		List<SubscribeListUserHolder> result = new ArrayList<SubscribeListUserHolder>();
		for (int i = 0; i < list.size(); i++) {
			result.add(new SubscribeListUserHolder(list.get(i)));
		}
		return result;
	}
	
	/**
	 * 取得置顶的最后一个元素的位置
	 * @return
	 */
	public int getTopLastIndex() {
		return -1;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder viewHolder;
		if (view == null) {
			viewHolder = new ViewHolder();
			view = this.mInflater.inflate(R.layout.subscribe_listview_user, null);
			viewHolder.setDateCreateView((TextView)view.findViewById(R.id.date_created));
			viewHolder.setTitleView((TextView)view.findViewById(R.id.title));
			viewHolder.setDescView((TextView)view.findViewById(R.id.description0));
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder)view.getTag();
		}
		SubscribeListUserHolder holder = list.get(position);
		viewHolder.getDateCreateView().setText(holder.getDateCreated());
		viewHolder.getTitleView().setText(holder.getProductNameKey());
		StringBuffer desc = new StringBuffer();
		if (StringUtils.isNotBlank(holder.getSaleTypeName())) {
			desc.append("销售方式:");
			desc.append(holder.getSaleTypeName());
		}
		if (StringUtils.isNotBlank(holder.getCreditName())) {
			desc.append(",公司信誉:");
			desc.append(holder.getCreditName());
		}
		if (StringUtils.isNotBlank(holder.getMarketarea())) {
			desc.append(",市场区域:");
			desc.append(holder.getMarketarea());
		}
		desc.append(",更新日期:");
		desc.append(holder.getLastUpdated());
		viewHolder.getDescView().setText(desc.toString().replaceFirst(",", ""));
		final String id = holder.getId();
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(context, SubscribeItemUserActivity.class);
				i.putExtra("id", id);
				i.putExtra("form", "items");
				context.startActivity(i);
			}
		});
		return view;
	}
	
	protected class ViewHolder {
		protected TextView dateCreateView;
		protected TextView titleView;
		protected TextView descView;
		public TextView getDateCreateView() {
			return dateCreateView;
		}
		public void setDateCreateView(TextView dateCreateView) {
			this.dateCreateView = dateCreateView;
		}
		public TextView getTitleView() {
			return titleView;
		}
		public void setTitleView(TextView titleView) {
			this.titleView = titleView;
		}
		public TextView getDescView() {
			return descView;
		}
		public void setDescView(TextView descView) {
			this.descView = descView;
		}
	}

}
